package com.hmusic.mymusic.utils;

import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.domain.Song;
import com.hmusic.mymusic.domain.SongList;
import com.hmusic.mymusic.service.SingerService;
import com.hmusic.mymusic.service.SongListService;
import com.hmusic.mymusic.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 图片/文件上传的工具类
 */
@Component
@Service
public class FileUpdateUtil {
    @Autowired
    private SingerService singerService;
    @Autowired
    private SongListService songListService;
    @Autowired
    private SongService songService;

    @Value("${songPic.dir}")
    private String songPicPath;

    @Value("${song.dir}")
    private String songPath;


    @Value("${singerAvator.dir}")
    private String singerAvatorPath;

    @Value("${songListPic.dir}")
    private String songListPath;

    @Value("${avatorImages.dir}")
    private String userAvatorPath;

    /**
     * 歌手头像上传
     */
    public String updateSingerPic(String id,MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        //文件名=当前时间毫秒+原来文件名
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = singerAvatorPath;
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + fileName);
        //储存到数据库中的文件地址
        String storeAvatorPath = "/img/singerPic/" + fileName;
        try {
            Singer singer1 = singerService.selectByPrimaryKey(id);
            String oldSingerAvatorUrl = singer1.getPic();
            String newSingerAvatorUrl = oldSingerAvatorUrl.substring(15);
            if (!newSingerAvatorUrl.equals("Firstavator.jpeg")) {
                File file2 = new File(filePath, newSingerAvatorUrl);
                if (file2.exists()) file2.delete();//删除原有地址

            }
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeAvatorPath;
    }

    /**
     * 歌单封面上传
     */
    public String updateSongListPic(String id,MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        //文件名=当前时间毫秒+原来文件名
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = songListPath;
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + fileName);
        //储存到数据库中的文件地址
        String storeAvatorPath = "/img/songListPic/" + fileName;
        try {
            SongList songList = songListService.selectByPrimaryKey(id);
            String oldSongListAvatorUrl = songList.getPic();
            String newSongListAvatorUrl = oldSongListAvatorUrl.substring(17);
            if (!newSongListAvatorUrl.equals("FirstSongListPic.jpeg")) {
                File file2 = new File(filePath, newSongListAvatorUrl);
                if (file2.exists()) file2.delete();//删除原有地址
            }
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeAvatorPath;
    }

    /**
     * 歌曲封面上传
     */
    public String updateSongPic(String id,MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        //文件名=当前时间毫秒+原来文件名
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = songPicPath;
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + fileName);
        //储存到数据库中的文件地址
        String storeAvatorPath = "/img/songPic/" + fileName;
        try {
            Song song = songService.selectByPrimaryKey(id);
            String oldSongAvatorUrl = song.getPic();
            String newSongAvatorUrl = oldSongAvatorUrl.substring(13);
            if (!newSongAvatorUrl.equals("songPic.jpeg")) {
                File file2 = new File(filePath, newSongAvatorUrl);
                if (file2.exists()) file2.delete();//删除原有地址
            }
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeAvatorPath;
    }

    /**
     * 歌曲地址更新
     */
    public String updateSong(String id,MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        //文件名=当前时间毫秒+原来文件名
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = songPath;
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + fileName);
        //储存到数据库中的文件地址
        String storeAvatorPath = "/song/" + fileName;
        try {
            Song song = songService.selectByPrimaryKey(id);
            String oldSongAvatorUrl = song.getPic();
            String newSongAvatorUrl = oldSongAvatorUrl.substring(6);
            File file2 = new File(filePath, newSongAvatorUrl);
            if (file2.exists()) file2.delete();//删除原有地址
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeAvatorPath;
    }

}
